package
{
	import UIComponent.DynamicLabel;
	import UIComponent.PointSprite;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayList;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Text;
	import mx.core.UIComponent;
	
	import skeletons.CaseSkeleton;
	import skeletons.ConclusionSkeleton;
	import skeletons.DKRelationSkeleton;
	import skeletons.DataSourceSkeleton;
	import skeletons.KConRelationSkeleton;
	import skeletons.KnowledgeSkeleton;
	
	import spark.primitives.Graphic;
	import spark.primitives.Line;
	
	import sprites.CaseSprite;
	import sprites.ConclustionSprite;
	import sprites.DKRelationSprite;
	import sprites.DataSourceSprite;
	import sprites.KConRelationSprite;
	import sprites.KnowledgeSprite;

	public class MyCanvas extends UIComponent
	{
		public var curProject:Project;
		static private var Y_UP:int=10;
		static private var X_CASELEFT:int=10;
		static private var X_DATALEFT:int=30;
		static private var CASE_HEIGHT:int=20;
		static private var X_KNOWLEFT:int=380;
		static private var X_CONLEFT:int=730;
		static private var Y_SPACE:int=5;
		
		private var selectedCase:CaseSprite;
		private var selectedData:DataSourceSprite;
		private var selectedKnow:KnowledgeSprite;
		private var selectedCon:ConclustionSprite;
		
		private var mouseDownObject:Object;
		private var trackingObject:PointSprite;
		
		private var selectedLinkType:Number=0; //0 - pro 1-con
		
		private var CASE_DATA_LINKER_COLOR:uint =0x01A9A8;
		private var ZOMBIE_LINE_COLOR:uint =0x707D7E;
		private var LIFE_LINE_COLOR:uint=0x028685;
		private var POISON_LINE_COLOR:int=0x516402;
		
		public function MyCanvas()
		{
			var g2d:Graphics=this.graphics;
			curProject=null;
		}
		
		public function loadProject(PID:int):void
		{
			curProject= new Project(this,PID);
		}
		
		public function unloadPorject():void
		{
			curProject=null;
		}
		
		private function layComponent():void
		{
			
			if(curProject==null)
				return;	
			var g2d:Graphics=this.graphics;

			//Case
			if(curProject.caseList==null)
				return;
					
			g2d.lineStyle(2.0,CASE_DATA_LINKER_COLOR);
			var curY:int=Y_UP;
			
			var i:int;
			for(i=0;i<curProject.caseList.length;i++)
			{
				var curCase:CaseSkeleton=(CaseSkeleton)(curProject.caseList.getItemAt(i));
				curCase.getSprite().x=X_CASELEFT;
				curCase.getSprite().y=curY;
				curY+=curCase.getSprite().getHeight()
					+Y_SPACE;
				
				//DataSource
				if(curCase.getDataSourceList()==null)
					continue;
				var j:int;
				var curData:DataSourceSkeleton;
				for(j=0;j<curCase.getDataSourceList().length;j++)
				{
					curData=(DataSourceSkeleton)(curCase.getDataSourceList().getItemAt(j));				
					curData.getSprite().x=X_DATALEFT;
					curData.getSprite().y=curY;
					curData.getSprite().invalidateDisplayList();
					
					//line
					var y:Number= curY + curData.getSprite().getHeight()/2;
					g2d.moveTo(X_CASELEFT+5,y-Y_SPACE - curData.getSprite().getHeight()/2);
					g2d.curveTo(X_CASELEFT+5,y, X_DATALEFT, y);
					
					curY+=curData.getSprite().getHeight()+Y_SPACE;
				}
				if(curData!=null){
					g2d.moveTo(X_CASELEFT+4, curCase.getSprite().y + curCase.getSprite().getHeight());
					g2d.lineTo(X_CASELEFT+4, curY-curData.getSprite().getHeight()-Y_SPACE);
				}
			}

			//Knowledge
			if(curProject.knowledgeList==null)
				return;
			curY=Y_UP;
			for(i=0;i<curProject.knowledgeList.length;i++)
			{
				var curKnow:KnowledgeSkeleton=(KnowledgeSkeleton)(curProject.knowledgeList.getItemAt(i));
				curKnow.getSprite().x=X_KNOWLEFT;
				curKnow.getSprite().y=curY;
				curY+=curKnow.getSprite().getHeight() + Y_SPACE;
				curKnow.getSprite().invalidateDisplayList();
			}
			
			//Conclusion
			if(curProject.conclusionList==null)
				return;
			curY=Y_UP;
			for(i=0;i<curProject.conclusionList.length;i++)
			{
				var curCon:ConclusionSkeleton=(ConclusionSkeleton)(curProject.conclusionList.getItemAt(i));
				curCon.getSprite().x=X_CONLEFT;
				curCon.getSprite().y=curY;
				curY+=curCon.getSprite().getHeight() + Y_SPACE;
				curCon.getSprite().invalidateDisplayList();
			}
		}
		
		//drawing relations
		private function lineWork():void
		{
			var g2d:Graphics=this.graphics;
			//data_knowledge_lines
			if(curProject.knowledgeList==null)
				return;
			var i:int;
			for(i=0;i<curProject.getDKRelationList().length;i++)
			{
				var curDK:DKRelationSkeleton = (DKRelationSkeleton)(curProject.getDKRelationList().getItemAt(i));
				//set position
				curDK.getSprite().updateLine();
				var line:Line=curDK.getSprite().getLine();
				var lenX:Number = line.xTo - line.xFrom;
				var lenY:Number = line.yTo - line.yFrom;
				
				var bold:Number=1;
				var color:uint = ZOMBIE_LINE_COLOR;
				if(curDK.getSprite().getSelected()==true)
				{
					bold=3;
				}
				if(curDK.getFromPointSkeleton().getSprite().getLifeStatus()!=0)
				{
					if(curDK.getType()==0)
					{
						color=LIFE_LINE_COLOR;
					}
					else
					{
						color=POISON_LINE_COLOR;
					}
				}
				g2d.lineStyle(bold,color);
				
				g2d.moveTo(line.xFrom ,line.yFrom);
				g2d.curveTo( line.xFrom + lenX /4 , line.yFrom , line.xFrom + lenX/2 , line.yFrom + lenY/2);
				g2d.curveTo( line.xTo - lenX/4, line.yTo, line.xTo, line.yTo);
				g2d.lineStyle(1,0x001111);				
			}
			//knowledge_conclusion_lines
			for(i=0;i<curProject.getKConRelationList().length;i++)
			{
				var curKCon:KConRelationSkeleton=(KConRelationSkeleton)(curProject.getKConRelationList().getItemAt(i));
				//set position
				curKCon.getSprite().updateLine();
				var aline:Line=curKCon.getSprite().getLine();
				var alenX:Number = aline.xTo - aline.xFrom;
				var alenY:Number = aline.yTo - aline.yFrom;
				
				var bold:Number=1;
				var color:uint = ZOMBIE_LINE_COLOR;
				if(curKCon.getSprite().getSelected()==true)
				{
					bold=3;
				}
				if(curKCon.getFromPointSkeleton().getSprite().getLifeStatus()!=0)
				{
					if(curKCon.getType()==0)
					{
						color=LIFE_LINE_COLOR;
					}
					else
					{
						color=POISON_LINE_COLOR;
					}
				}
				g2d.lineStyle(bold,color);
				
				g2d.moveTo(aline.xFrom ,aline.yFrom);
				g2d.curveTo( aline.xFrom + alenX /4 , aline.yFrom , aline.xFrom + alenX/2 , aline.yFrom + alenY/2);
				g2d.curveTo( aline.xTo - alenX/4, aline.yTo, aline.xTo, aline.yTo);
				g2d.lineStyle(1,0x001111);
			}	
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			if(curProject ==null)
				return;
			this.resetVisual();
			this.colorAll();
			this.highlight();
			this.graphics.clear();
			this.layComponent();
			this.lineWork();
		}

		//Getters and Setters
		public function getSelectedCase():CaseSprite
		{
			return selectedCase;
		}

		public function setSelectedCase(value:CaseSprite):void
		{
			selectedCase = value;
		}

		public function getSelectedData():DataSourceSprite
		{
			return selectedData;
		}

		public function setSelectedData(value:DataSourceSprite):void
		{
			selectedData = value;
		}

		public function getSelectedKnow():KnowledgeSprite
		{
			return selectedKnow;
		}

		public function setSelectedKnow(value:KnowledgeSprite):void
		{
			selectedKnow = value;
		}

		public function getSelectedCon():ConclustionSprite
		{
			return selectedCon;
		}

		public function setSelectedCon(value:ConclustionSprite):void
		{
			selectedCon = value;
		}

		public function getMouseDownObject():Object
		{
			return mouseDownObject;
		}

		public function setMouseDownObject(value:Object):void
		{
			mouseDownObject = value;
		}

		public function getTrackingObject():PointSprite
		{
			return trackingObject;
		}

		public function setTrackingObject(value:PointSprite):void
		{
			trackingObject = value;
		}

		public function getSelectedLinkType():Number
		{
			return selectedLinkType;
		}

		public function setSelectedLinkType(value:Number):void
		{
			selectedLinkType = value;
		}
		
		//reset all visual features
		public function resetVisual():void
		{
			var i:int=0;
			
			//datasource
			var caseList:ArrayList = curProject.caseList;
			if(caseList !=null)
			{
				for(i=0;i<caseList.length;i++)
				{
					var dataList:ArrayList= (CaseSkeleton)(caseList.getItemAt(i)).getDataSourceList();
					if(dataList == null)
						continue;
					
					var j:int=0;
					for(j=0;j<dataList.length; j++)
					{
						(DataSourceSkeleton)(dataList.getItemAt(j)).getSprite().resetHighlight();
						(DataSourceSkeleton)(dataList.getItemAt(j)).getSprite().resetColor();
					}
				}
			}
			
			//knowledge
			var knowList:ArrayList = curProject.knowledgeList;
			if(knowList!=null)
			{
				for(i=0;i<knowList.length;i++)
				{
					(KnowledgeSkeleton)(knowList.getItemAt(i)).getSprite().resetHighlight();
					(KnowledgeSkeleton)(knowList.getItemAt(i)).getSprite().resetColor();
				}
			}
			
			//conclusion
			var conList:ArrayList = curProject.conclusionList;
			if(conList!=null)
			{
				for(i=0; i<conList.length; i++)
				{
					(ConclusionSkeleton)(conList.getItemAt(i)).getSprite().resetHighlight();
					(ConclusionSkeleton)(conList.getItemAt(i)).getSprite().resetColor();
				}
			}
			
			//DK
			var DKList:ArrayList = curProject.getDKRelationList();
			if(DKList!=null)
			{
				for(i=0; i<DKList.length; i++)
				{
					(DKRelationSkeleton)(DKList.getItemAt(i)).getSprite().setSelected(false);
				}
			}
			
			//KCon
			var KConList:ArrayList = curProject.getKConRelationList();
			if(KConList!=null)
			{
				for(i=0; i<KConList.length; i++)
				{
					(KConRelationSkeleton)(KConList.getItemAt(i)).getSprite().setSelected(false);
				}
			}
			
		}
		//do coloring
		public function colorAll():void
		{
			var caseList:ArrayList = curProject.caseList;
			if(caseList==null)
				return;
			
			var i:int=0;
			for(i=0;i<caseList.length;i++)
			{
				var dataList:ArrayList = (CaseSkeleton)(caseList.getItemAt(i)).getDataSourceList();
				if(dataList==null)
					continue;
				var j:int=0;
				for(j=0;j<dataList.length;j++)
				{
					(DataSourceSkeleton)(dataList.getItemAt(j)).getSprite().colorAfter();
				}
			}
		}
		//do highlighting
		public function highlight():void
		{
			if(trackingObject == null)
				return;
			
			trackingObject.setHighlight(true);
			if(trackingObject is DataSourceSprite)
			{
				trackingObject.highlightAfter(true);
			}
			else if(trackingObject is KnowledgeSprite)
			{
				trackingObject.highlightBefore(true);
				trackingObject.highlightAfter(true);
			}
			else
			{
				trackingObject.highlightBefore(true);
			}
		}
	}
}